﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using Catering_Rules;
using Microsoft.Office.Core;
using System.Drawing;
using System.IO;

namespace Catering.Catering_Page.richang
{
    public partial class YearKq : System.Web.UI.Page
    {
        private static AttendanceRecord ar = new AttendanceRecord();
        #region 页面加载
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        #endregion

        protected void But_Moths_Seacher_Click(object sender, EventArgs e)
        {
            string AREAID = UCDDLToolWithDate1.GetDDL_Area();
            string CLASSID = UCDDLToolWithDate1.GetDDL_Class();
            DateTime DTIME = UCDDLToolWithDate1.GetDateTime();
            string NAME = txt_name.Text;
            string PNAME = txt_gw.Text;
            string pid = "0";
            if (Session["UserID"] != null) pid = Session["UserID"].ToString();
            DataTable dt = ar.GetKaoQin(pid, AREAID, CLASSID, DTIME, NAME, PNAME);
            Repeater1.DataSource = dt;
            Repeater1.DataBind();
        }

        protected void but_output_Moths_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Excel.Application excel1 = null;
            Microsoft.Office.Interop.Excel._Worksheet ws = null;
            Microsoft.Office.Interop.Excel.Range canbie = null;
            string stick = DateTime.Now.Ticks.ToString();

            try
            {
                excel1 = new Microsoft.Office.Interop.Excel.Application();
                excel1.Application.Workbooks.Add(true);//引用Excel工作簿
                //Microsoft.Office.Interop.Excel._Worksheet ws = new Microsoft.Office.Interop.Excel.WorksheetClass();
                ws = new Microsoft.Office.Interop.Excel.WorksheetClass();
                ws = (Microsoft.Office.Interop.Excel._Worksheet)excel1.ActiveSheet;//获取当前活动工作薄
                //Microsoft.Office.Interop.Excel.Range canbie;//创建Range操作一行作为标题
                excel1.Visible = false;
                int YearMonthNum = 12;//每年月数

                #region  表头

                string groupid = UCDDLToolWithDate1.GetDDL_Area();
                string classid = UCDDLToolWithDate1.GetDDL_Class();
                DateTime DTIME = UCDDLToolWithDate1.GetDateTime();
                string NAME = txt_name.Text;
                string PNAME = txt_gw.Text;
                if (DTIME.Year == 1949) return;
                string pid = "0";
                if (Session["UserID"] != null) pid = Session["UserID"].ToString();
                DataTable dt = ar.GetKaoQin(pid, groupid, classid, DTIME, NAME, PNAME);
                string title = "";
                if (groupid != "-10")
                {
                    Catering_Model.OperationArea tempgroup = new Catering_Rules.OperationArea().GetModel(Convert.ToInt32(groupid));
                    if (tempgroup != null)
                    {
                        title += tempgroup.AreaName;
                    }
                }
                if (classid != "-10")
                {
                    Catering_Model.ClassInfo tempclass = new Catering_Rules.ClassInfo().GetModel(Convert.ToInt32(classid));
                    if (tempclass != null)
                    {
                        title += tempclass.CName + "班组";
                    }
                }
                title += DTIME.Year + "年年考勤统计";

                canbie = ws.get_Range(ws.Cells[1, 1], ws.Cells[1, YearMonthNum + 4]);
                canbie.MergeCells = true;//合并单元格
                canbie.HorizontalAlignment = XlVAlign.xlVAlignCenter;//居中
                canbie.Font.Bold = 2;
                ws.Cells[1, 1] = title;


                #endregion

                #region 第一行标题

                ws.Cells[2, 1] = "序号";
                ws.Cells[2, 2] = "姓名";
                ws.Cells[2, 3] = "岗位";
                for (int i = 1; i <= YearMonthNum; i++)
                {
                    ws.Cells[2, (3 + i)] = i + "月";
                }
                ws.Cells[2, (4 + YearMonthNum)] = "出勤";
                canbie = ws.get_Range(ws.Cells[2, 1], ws.Cells[2, (4 + YearMonthNum)]);
                canbie.Interior.Color = ColorTranslator.ToWin32(Color.LightGray);

                #endregion

                #region 主要内容

                int canbiehangshu = 3;//餐别行数
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        string name = dt.Rows[i]["Name"].ToString();
                        string pname = dt.Rows[i]["PName"].ToString();
                        ws.Cells[canbiehangshu, 1] = (i + 1);
                        ws.Cells[canbiehangshu, 2] = name;
                        ws.Cells[canbiehangshu, 3] = pname;
                        for (int j = 1; j <= YearMonthNum; j++)
                        {
                            ws.Cells[canbiehangshu, (3 + j)] = dt.Rows[i]["D" + j].ToString();
                        }
                        ws.Cells[canbiehangshu, (4 + YearMonthNum)] = dt.Rows[i]["CountKaoQin"].ToString();
                        canbiehangshu++;
                    }
                    dt.Dispose();
                }
                ws.Cells.EntireColumn.AutoFit();
                canbie = ws.get_Range(ws.Cells[1, 1], ws.Cells[canbiehangshu - 1, (4 + YearMonthNum)]);
                canbie.Borders.LineStyle = 1;
                canbie.HorizontalAlignment = XlVAlign.xlVAlignCenter;//居中
                #endregion

                if (!Directory.Exists(Server.MapPath("../../xls_template"))) Directory.CreateDirectory(Server.MapPath("../../xls_template"));
                ws.SaveAs(Server.MapPath("../../xls_template") + "\\" + stick + ".xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlAddIn8);
            }
            finally
            {
                if (excel1 != null)
                {
                    if (ws != null)
                    {
                        if (canbie != null)
                        {
                            System.Runtime.InteropServices.Marshal.ReleaseComObject(canbie);
                            canbie = null;

                        }
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
                        ws = null;
                    }
                    excel1.Application.Workbooks.Close();
                    excel1.Quit();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(excel1);
                    Toolss.KillExcel(excel1);
                    excel1 = null;
                    GC.Collect();
                }
            }

            #region 发送

            try
            {
                string path = Server.MapPath("../../xls_template") + "\\" + stick + ".xls";
                System.IO.FileInfo file = new System.IO.FileInfo(path);
                Response.Clear();
                Response.Charset = "GB2312";
                Response.ContentEncoding = System.Text.Encoding.UTF8;
                // 添加头信息，,弹出另存为窗口  
                Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
                //让浏览器头显示下载信息  
                Response.AddHeader("Content-Length", file.Length.ToString());
                // 指定返回一个不能被客户端读取的流，下载  
                Response.ContentType = "application/ms-excel";
                //把文件流下载到客户端  
                Response.WriteFile(file.FullName);
                Toolss.DeleteFile(Server.MapPath("../../xls_template") + "\\" + stick + ".xls");
                // 停止页面的执行
                Response.End();
            }
            catch
            {
            }

            #endregion

        }

    }
}